var token = null;
layui.config({
	base: 'module/'
}).use(['sdDialog', 'sdHttp', 'sdLocal', 'sdEncrypt', 'form'], () => {
	let sdDialog = layui.sdDialog;
	let sdLocal = layui.sdLocal;
	let sdHttp = layui.sdHttp;
	let sdEncrypt = layui.sdEncrypt;
	let form = layui.form;

	let login = {
		enterApp(app) {
			let param = {
				url: "/v1/platform/iam/application",
				callback: function(d) {
					sdLocal.token.put(d.token);
					sdLocal.user.put(d.user);
					location.replace('./');
				},
				error: function(d) {
					alert("服务器异常请稍后再试")
					location.replace('./login.html');
				}
			}
			sdHttp.get(param, app.id);
		},
		img: function() {
			let param = {
				url: "/v1/platform/iam/code",
				callback: function(d) {
					token = d.token;
					$('.login-captcha').attr('src', d.img);
				},
				error: function(d) {
					alert(d)
				}
			}
			sdHttp.get(param)
		},
		bg: function() {
			// 背景动画效果
			const stars = document.querySelectorAll('div');
			stars.forEach((star, i) => {
				let x = `${Math.random() * 360}vmax`
				let y = `${Math.random() * 200}vh`
				let z = `${Math.random() * 200 - 50}vmin`
				let rx = `${Math.random() * 360}deg`
				star.style.setProperty('--x', x)
				star.style.setProperty('--y', y)
				star.style.setProperty('--z', z)
				star.style.setProperty('--rx', rx)
			});

			if (document.documentElement.classList.contains('hyper')) {
				stars.forEach((star, i) => {
					star.style.animationName = null;
					//star.style.animationIterationCount = 1;
				});
			} else {
				stars.forEach((star, i) => {
					let delay = `${Math.random() * 900}ms`;
					star.style.animationDelay = delay;
					star.style.animationName = 'hyper';
				});
			}
			document.documentElement.classList.toggle('hyper');
		},
		submit: function(data) {
			data.password = sdEncrypt.sha1(data.password);
			let param = {
				url: '/v1/platform/iam/login',
				data: data,
				token: token,
				callback: function(d) {
					sdLocal.token.put(d.token);
					sdLocal.user.put(d.user);
					login.img();
					$('#login-form-view').hide();
					$('#application-list-view').show();
					// 读取子系统列表
					login.app();
				},
				error: function() {
					login.img();
				}
			}
			sdHttp.post(param);
		},
		app: function() {
			// 记住上次登录的平台
			if (sdLocal.app.get()) {
				login.enterApp(sdLocal.app.get())
			} else {
				let param = {
					url: "/v1/platform/application",
					callback: function(d) {
						if (d.length == 1) {
							sdLocal.app.put(d[0]);
							login.enterApp(d[0])
						} else {
							$('#login-app-list-view').vm({
								apps: d
							})
							
							$('#login-app-list-view').on('click', '.app-in', function(event) {
								let data = $(event)[0].currentTarget.dataset;
								sdLocal.app.put(data);
								login.enterApp(data)
							})
							$('#application-list-view').on('click', '#exit-system', function(event) {
								sdDialog.confirm({
									title: '确定要退出登录吗?',
									submit() {
										sdLocal.clean();
										location.replace('/login.html');
									}
								});
							})
						}
					}
				}
				sdHttp.get(param)
			}
		}
	}

	if (sdLocal.token.get()) {
		$('#login-form-view').hide();
		$('#application-list-view').show();
		login.app();
		return;
	}

	login.img();
	// 表单提交监听
	form.on('submit(login-submit)', function (obj) {
	    let field = obj.field;
	    login.submit(field)
	});

	// 图形验证码
	$('.login-captcha').click(function() {
		login.img();
	});
	// 回车事件
	$(document).keydown(function(event) {
		if (event.keyCode == 13) {
			$('#login-submit').click();
		}
	});

	// 背景动画
	// login.bg();

});
